package com.cwk.yygh.msm.service.impl;

import com.cwk.yygh.msm.service.MsmService;
import com.cwk.yygh.vo.msm.MsmVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MsmServiceImpl implements MsmService {
    @Resource
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Override
    public boolean send(String phone, String code) {
        if(StringUtils.isEmpty(phone))
            return false;
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        try{
            helper.setTo(phone);//收件人
            helper.setFrom(from);//发件人
            if(code.length()==6)
                helper.setSubject("验证码");//主题
            else
                helper.setSubject("预约提醒");
            helper.setText(code);//内容
            javaMailSender.send(mimeMessage);
        }catch (MessagingException e){
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public boolean send(MsmVo msmVo) {
        System.out.println(msmVo);
        if(!StringUtils.isEmpty(msmVo.getPhone())){
            boolean isSend = this.send(msmVo.getPhone()+"@qq.com",
                    "病人："+msmVo.getParam().get("name")+"，就诊："+msmVo.getParam().get("title")+"，时间："+msmVo.getParam().get("reserveDate"));
            return isSend;
        }
        return true;
    }
}
